package com.gxs.ea.fileupload.beans.jsf.company;

import java.io.Serializable;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.gxs.ea.fileportal.domain.Field;

@Component
@Scope("session")
public class FieldConverter implements Converter, Serializable
{
	@Autowired
	ViewCompanyBean viewCompanybean;

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String fieldName) 
	{
		List<Field> list = viewCompanybean.getSelectedCompany().getCompanyMetaConfig().getFields();
		for( Field f : list)
		{
			if(f.getFieldName().equals(fieldName))
				return f;
		}
		
		return null;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object obj) 
	{
		Field f = (Field) obj;
		return f.getFieldName();
	}

	public ViewCompanyBean getViewCompanybean() {
		return viewCompanybean;
	}

	public void setViewCompanybean(ViewCompanyBean viewCompanybean) {
		this.viewCompanybean = viewCompanybean;
	}
	
	

}
